<?php

class M_materi extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	function create($nama_file,$judul,$judul_seo, $deskripsi,$hits,$id_divisi){
		$data = array(
		'nama_file' =>$nama_file,
		'judul'=> $judul,
		'judul_seo' => $judul_seo,
		'deskripsi' => $deskripsi,
		'hits' => $hits,
		'id_divisi' => $id_divisi
		);
	
		$this->db->insert('materi', $data);
		if ($this->db->affected_rows() == 1) {
			return TRUE;
		}
		return FALSE;
	}
	
	function getTable($perPage = 0, $offset = 0) {
        $this->db->select('*');
		$this->db->from('materi');
		$this->db->join('divisi','divisi.id_divisi=materi.id_divisi');
		$this->db->order_by('id_materi', 'DESC');
        $this->db->limit($perPage, $offset);
        $query = $this->db->get();

        return $query->result_array();
        
    }
	
	function getAllData(){
		$this->db->select('*');
		$this->db->order_by('id_materi', 'DESC');
        $query = $this->db->get('materi');
		
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
	}
	
	function getDataById($id){
		//$this->db->select('*');
		//$this->db->where('id_materi', $id);
		//$query = $this->db->get('materi');
		//if($query->num_rows() == 1){
			//return $query->result_array();
		//}
		$query = $this->db->query("select * from materi a join divisi b on a.id_divisi=b.id_divisi where a.id_materi='".$id."' order by a.id_materi");
		return $query->result_array();

	}
	
	function update($id,$nama_file,$judul,$judul_seo, $deskripsi,$id_divisi){
		$data = array(
		'nama_file' =>$nama_file,
		'judul'=> $judul,
		'judul_seo' => $judul_seo,
		'deskripsi' => $deskripsi,
		'id_divisi' => $id_divisi
		);
		$this->db->where('id_materi', $id);
		$this->db->update('materi', $data);
		
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function delete($id){
		$this->db->where('id_materi', $id);
		$this->db->delete('materi');
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function getAllDivisi(){
		$this->db->select('*');
		$query = $this->db->get('divisi');
		if($query->num_rows() > 0){
			return $query->result_array();
		}
		return FALSE;
	}
	
	function search($key){
		$this->db->select('*');
		$this->db->from('materi');
		$this->db->join('divisi', 'divisi.id_divisi = materi.id_divisi');
		$this->db->like('materi.judul', $key);
		$this->db->order_by('materi.id_materi', 'DESC');
		$query = $this->db->get ();
		
		return $query->result_array();
	}

}